//
//  RBContentTypeVC.m
//  RBDemoProj
//
//  Created by Lan on 2019/1/3.
//  Copyright © 2019年 SummerTea. All rights reserved.
//

#import "RBContentTypeVC.h"
#import "UIScrollView+RBAdd.h"
#import <Masonry.h>
#import "RBTextView.h"
#import "RBDefine.h"
#import "UIView+RBAdd.h"
#import <NSString+YYAdd.h>
#import <IQKeyboardManager.h>

@interface RBContentTypeVC ()

@property(nonatomic, strong) UIScrollView *scrollView;
@property(nonatomic, strong) RBTextView *textView;
@property(nonatomic, strong) UILabel *numL;

@end

@implementation RBContentTypeVC

#pragma mark - Lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setup];
}

#pragma mark - Private

- (void)setup
{
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.textView.placeHolder = self.placeHolder;
    self.textView.text = self.text;
    [self.scrollView rbSetContentWidth:kRBMainScreenWidth];
    [self.scrollView addView:self.textView withHeight:100 width:CGFLOAT_MIN top:8 bottom:8 left:10 right:10 centerX:NO];
    if (self.maxLength)
    {
        [self.scrollView addView:self.numL withHeight:25 width:200 top:0 bottom:10 left:CGFLOAT_MIN right:8 centerX:NO];
    }
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"确定" style:UIBarButtonItemStylePlain target:self action:@selector(confirmBtnTapped)];
}

#pragma mark - Event

- (void)confirmBtnTapped
{
    [self.view endEditing:YES];
    
    NSString *text = [self.textView.text stringByTrim];
    if(text.length < self.minLength)
    {
        [self.view showTip:[NSString stringWithFormat:@"请至少输入%zd字符", self.minLength] completion:nil];
        return;
    }
    if (self.maxLength && text.length > self.maxLength)
    {
        [self.view showTip:[NSString stringWithFormat:@"输入内容不能超过%zd字符", self.maxLength] completion:nil];
        return;
    }
    if (self.completion)
    {
        self.completion(text);
    }
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - Getter

- (UIScrollView *)scrollView
{
    if (!_scrollView)
    {
        self.scrollView = ({
            UIScrollView *view = [UIScrollView new];
            kRBIgnoreScrollViewInsetsAdjust(view);
            [self.view addSubview:view];
            [view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.edges.mas_equalTo(0);
            }];
            view;
        });
    }
    return _scrollView;
}

- (RBTextView *)textView
{
    if (!_textView)
    {
        self.textView = ({
            RBTextView *view = [RBTextView new];
            view.backgroundColor = [UIColor colorWithWhite:0.95 alpha:1];
            [view enableAutoGrowWithConsWidth:kRBMainScreenWidth - 20 minHeight:100 maxHeight:300];
            __weak typeof(self) weak_self = self;
            view.textChangeBlock = ^(NSString *text) {
                if (weak_self.maxLength)
                {
                    weak_self.numL.text = [NSString stringWithFormat:@"%zd/%zd", text.length, weak_self.maxLength];
                }
            };
            view;
        });
    }
    return _textView;
}

- (UILabel *)numL
{
    if (!_numL)
    {
        self.numL = ({
            UILabel *view = [UILabel new];
            view.font = [UIFont systemFontOfSize:13];
            view.numberOfLines = 1;
            view.textAlignment = NSTextAlignmentRight;
            view.text = [NSString stringWithFormat:@"%zd/%zd", self.textView.text.length, self.maxLength];
            view;
        });
    }
    return _numL;
}

@end
